package com.sls.game.parser.nutsnet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sls.game.pojo.NutsNetTradeResInfo;

/**
 * @Description 以SAX方式解析xml字符串
 * @author Wangxuegang
 * @date 2012-09-04
 */
public class TradeResponseSaxParseXml extends DefaultHandler {

	private List<NutsNetTradeResInfo> tradeResList = null;

	private NutsNetTradeResInfo tradeRes;

	// 用来存放每次遍历后的元素名称(节点名称)
	private String tagName;

	private static Logger log = Logger
			.getLogger(TradeResponseSaxParseXml.class);

	/**
	 * 对返回的xml格式字符串数据进行解析
	 * 
	 * @param resultStr
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws DocumentException
	 */
	public static List<NutsNetTradeResInfo> parseXmlString(String xmlStr)
			throws UnsupportedEncodingException {
		String encodeXmlStr = EncodeChineseCode.encodeChineseCodeOfGBK(xmlStr);
		// 加载资源文件 转化为一个输入流
		byte[] xmlStrByte = encodeXmlStr.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(xmlStrByte);
		try {
			// 构建SAXParser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();

			TradeResponseSaxParseXml handler = new TradeResponseSaxParseXml();
			parser.parse(in, handler);
			return handler.getTradeResList();

		} catch (ParserConfigurationException ep) {
			log.error(ep);
		} catch (SAXException es) {
			log.error(es);
		} catch (IOException e) {
			log.error(e);
		}

		return null;
	}

	// 只调用一次 初始化list集合
	@Override
	public void startDocument() throws SAXException {
		tradeResList = new ArrayList<NutsNetTradeResInfo>();
	}

	// 调用多次 开始解析
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("response")) {
			tradeRes = new NutsNetTradeResInfo();
		}
		this.tagName = qName;
	}

	// 调用多次
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("response")) {
			this.tradeResList.add(this.tradeRes);
		}
		this.tagName = null;
	}

	// 只调用一次
	@Override
	public void endDocument() throws SAXException {
	}

	// 调用多次
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (this.tagName != null) {
			String data = new String(ch, start, length);
			if (this.tagName.equals("ptOrderNo")) {
				this.tradeRes.setPtOrderNo(data);
			} else if (this.tagName.equals("status")) {
				this.tradeRes.setStatus(data);
			} else if (this.tagName.equals("supOrderNo")) {
				this.tradeRes.setSupOrderNo(data);
			} else if (this.tagName.equals("supOrderSuccessTime")) {
				this.tradeRes.setSupOrderSuccessTime(data);
			} else if (this.tagName.equals("supOrderDetail")) {
				try {
					data = URLDecoder.decode(data, "GBK"); // 进行GBK解码
				} catch (UnsupportedEncodingException e) {
					log.error(e);
				}
				this.tradeRes.setSupOrderDetail(data);
			} else if (this.tagName.equals("failedCode")) {
				this.tradeRes.setFailedCode(data);
			} else if (this.tagName.equals("failedReason")) {
				try {
					data = URLDecoder.decode(data, "GBK"); // 进行GBK解码
				} catch (UnsupportedEncodingException e) {
					log.error(e);
				}
				this.tradeRes.setFailedReason(data);
			}
		}
	}

	public NutsNetTradeResInfo getTradeRes() {
		return tradeRes;
	}

	public void setTradeRes(NutsNetTradeResInfo tradeRes) {
		this.tradeRes = tradeRes;
	}

	public List<NutsNetTradeResInfo> getTradeResList() {
		return tradeResList;
	}

	public void setTradeResList(List<NutsNetTradeResInfo> tradeResList) {
		this.tradeResList = tradeResList;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	// public static void main(String[] args) throws
	// UnsupportedEncodingException{
	// String xmlStr = "<?xml version=\"1.0\" encoding=\"GBK\"?><response>" +
	// "<ptOrderNo>aaa</ptOrderNo><status>bbb</status>" +
	// "<supOrderNo>ccc</supOrderNo>" +
	// "<supOrderSuccessTime>ddd</supOrderSuccessTime>" +
	// "<supOrderDetail>坚果网络科技</supOrderDetail>" +
	// "<failedCode>fff</failedCode>" +
	// "<failedReason>杭州手拉手</failedReason>" +
	// "</response>";
	// String str = EncodeChineseCode.encodeChineseCodeOfGBK(xmlStr);
	// List<NutsNetTradeResInfo> tradeResList =
	// parseXmlString(str);
	// if (tradeResList.size() > 0) {
	// for (NutsNetTradeResInfo tradeInfo:tradeResList) {
	// log.info(tradeInfo.getPtOrderNo());
	// log.info(tradeInfo.getStatus());
	// log.info(tradeInfo.getSupOrderNo());
	// log.info(tradeInfo.getSupOrderSuccessTime());
	// log.info(tradeInfo.getSupOrderDetail());
	// log.info(tradeInfo.getFailedCode());
	// log.info(tradeInfo.getFailedReason());
	// }
	// }
	// }

}
